//
//  BHSportCreateTitleView.swift
//  BigHealth
//
//  Created by 蔺百好 on 2021/7/16.
//

import UIKit
protocol BHSportCreateTitleViewDelegate {
    func didCreateTitleConfirm(text: String, type: Int)
}


class BHSportCreateTitleView: UIView {
    var delegate: BHSportCreateTitleViewDelegate?
    
    
    fileprivate let height: CGFloat = (UIScreen.safeBottomHeight+207)
    
    lazy var content_backView: UIView = {
        let view = UIView.init()
        view.backgroundColor = .white
        return view
    }()
    
    lazy var title_label: UILabel = {
        let label = UILabel.init()
        label.text = "运动主题"
        label.textColor = .color_333333
        label.font = .systemFont(ofSize: 16, weight: .bold)
        label.textAlignment = .center
        return label
    }()
    
    lazy var button: UIButton = {
        let btn = UIButton.init(type: .custom)
        btn.setTitle("确认", for: .normal)
        btn.backgroundColor = .color_34CE98
        btn.setTitleColor(.white, for: .normal)
        btn.titleLabel?.font = .systemFont(ofSize: 16, weight: .bold)
        btn.addTarget(self, action: #selector(confirmCreateSport), for: .touchUpInside)
        return btn
    }()
    
    lazy var text_backView: UIView = {
        let view = UIView.init()
        view.backgroundColor = .color_f8f8f8
        return view
    }()
    
    lazy var text_filed: UITextField = {
        let text = UITextField.init()
        text.font = .systemFont(ofSize: 14, weight: .medium)
        text.textColor = .color_333333
        text.textAlignment = .center
        text.placeholder = "为这次运动创建一个主题吧～"
        return text
    }()
    
    lazy var shadow_view: UIView = {
        let view = UIView.init()
        view.backgroundColor = UIColor.black.withAlphaComponent(0.4)
        return view
    }()
    
    
    var type: Int = 1
    
    deinit {
        NotificationCenter.default.removeObserver(self)
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        
        NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillChangeFrame(node:)), name: UIResponder.keyboardWillChangeFrameNotification, object: nil)

 
        self.isHidden = true
        self.addSubview(self.shadow_view)
        self.addSubview(self.content_backView)
        
        self.content_backView.addSubview(self.title_label)
        self.content_backView.addSubview(self.text_backView)
        self.content_backView.addSubview(self.text_filed)
        self.content_backView.addSubview(self.button)
        
        self.configFrame()
        
        self.text_backView.addLayerCorner(4, 0, .white)
        self.button.addLayerCorner(22, 0, .white)
        
        self.content_backView.addCorner(conrners: [.topLeft,.topRight], radius: 12)
        
        self.shadow_view.addViewAction {[weak self] (_) in
            self?.hide()
        }
    }
    
    private func configFrame() {
        self.shadow_view.frame = CGRect(x: 0, y: 0, width: UIScreen.width, height: UIScreen.height)
        self.content_backView.frame = CGRect(x: 0, y: UIScreen.height, width: UIScreen.width, height: self.height)
        self.title_label.frame = CGRect(x: (UIScreen.width-200)/2.0, y: 30, width: 200, height: 18)
        
        self.text_backView.frame = CGRect(x: 16, y: 75, width: UIScreen.width-32, height: 48)
        self.text_filed.frame = CGRect(x: 32, y:80, width: UIScreen.width-64, height: 38)
        self.button.frame = CGRect(x: 16, y: 153, width: UIScreen.width-32, height: 44)
    }
    
    @objc func keyboardWillChangeFrame(node : Notification){
            
        print(node.userInfo ?? "")
        // 1.获取动画执行的时间
        let duration = node.userInfo?[UIResponder.keyboardAnimationDurationUserInfoKey] as! TimeInterval
            
        // 2.获取键盘最终 Y值
        let endFrame = (node.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
        let y = endFrame.origin.y
            
        //3计算工具栏距离底部的间距
        let margin = UIScreen.main.bounds.height - y
       
        UIView.animate(withDuration: duration) {
            self.content_backView.frame = CGRect(x: 0, y: UIScreen.height-self.height-margin, width: UIScreen.width, height: self.height)
        }
    }
    
    
    func show() {
        self.isHidden = false
        self.superview?.bringSubviewToFront(self)
        UIView.animate(withDuration: 0.25) {
            self.content_backView.frame = CGRect(x: 0, y: UIScreen.height-self.height, width: UIScreen.width, height: self.height)
        }
    }
    
    
    func hide()  {
        self.endEditing(true)
        UIView.animate(withDuration: 0.25) {
            self.content_backView.frame = CGRect(x: 0, y: UIScreen.height, width: UIScreen.width, height: self.height)
        } completion: { (finished) in
            self.isHidden = true
        }
    }
    
    @objc func confirmCreateSport() {
        
        self.delegate?.didCreateTitleConfirm(text: self.text_filed.text ?? "", type: self.type)
    }
    
    func clear() {
        self.endEditing(true)
        self.text_filed.text = ""
        self.hide()
    }

}
